<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Menu
 *
 * @author aneto
 */
abstract class NavigationMenu {
	
	protected $elements = array();
	protected $activeElements;

	static private $instances;

	const DEFAULT_MENU = 1;
	const ADMIN_ZONE_MENU = 2;
	const HEADER_MENU = 3;
	const SUB_MENU = 4;
	const FOOTER_MENU = 5;
	const ADMIN_ZONE_MENU_WITH_PERMISSIONS = 6;

	abstract protected function initElements();


	protected function __construct(array $activeElements){

		$this->init($activeElements);
	}


	public function getElements(){
		return $this->elements;
	}

	public function getElement($type, $id){
		$elems = $this->getElements();

		while($elems){
			$elem = array_pop($elems);
			if($elem->getType() == $type && $elem->getId() == $id){
				return $elem;
			}

			if($elem->getType() == MenuElement::STRUCTURE_NODE)
				$elems = array_merge($elems, $elem->getSubElements());
		}

		return false;


	}

	private function init($activeElements){
		foreach($activeElements as $e){
			$ae = $this->activeElement($e);
			if($ae)
				$this->activeElements[] = $ae;
		}

		$this->initElements();
				
	}


	public function isActive(MenuElement $me){

		$result = false;

	
		foreach($this->activeElements as $activeElement){
			if($activeElement instanceof ActiveMenuElement){
				$result =  $result || $activeElement->isChildrenOf($me);
			}
		}

		return $result;
	}

	public function getCulture(){
		return self::getAction()->getUser()->getCulture();
	}


	public function getAbbr(){
		return self::getAction()->getRoute()->getObject()->getLang();
	}


	protected function activeElement($object){
		if($object instanceof Structure){
			return new StructureActiveMenuElement($object);
		}else if($object instanceof ProductGroup){
			return new ProductGroupActiveMenuElement($object);
		}else{
			return false;
		}
	}


	public function getAction(){
		return sfContext::getInstance()->getController()
					->getAction( sfContext::getInstance()->getRequest()->getParameter('module'), sfContext::getInstance()->getRequest()->getParameter('action') );
	}
	

	static function getInstance($type){

		if(!isset(self::$instances[$type])){
			switch($type){
				case self::DEFAULT_MENU:
					self::$instances[$type] = new NavigationDefaultMenu(array());
					break;
				case self::ADMIN_ZONE_MENU:
					self::$instances[$type] = new NavigationAdminMenu(array());
					break;
				case self::HEADER_MENU:
					self::$instances[$type] = new NavigationHeaderMenu(array());
					break;
				case self::SUB_MENU:
					self::$instances[$type] = new NavigationSubMenu(array());
					break;
				case self::FOOTER_MENU:
					self::$instances[$type] = new NavigationFooterMenu(array());
					break;
				case self::ADMIN_ZONE_MENU_WITH_PERMISSIONS:
					self::$instances[$type] = new NavigationAdminMenuWithPermissions(array());
					break;
				default:
					throw new Exception('wrong menu type');
			}
		}

		return self::$instances[$type];

	}

	
}